@extends('layouts.wechat.application')
@section('content')
    <div id="wrapper">
        <div class="cart-index">
            <div style="height:1rem;"></div>

            <div class="cart-index-wrap" id="more" @if(!$carts->isEmpty()) style="display: none" @endif>
                <div class="empt">
                    <div class="b3">
                        <a href="product/category" class="ui-button ui-button-disable">
                            <span>全部商品</span>
                        </a>
                        <a href="/" class="ui-button">
                            <span>精选商品</span>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="cart-index" id="carts" @if($carts->isEmpty()) style="display: none" @endif>
            <div class="cart-index-wrap">
                <div class="cart-list">

                    <ul>
                        @foreach($carts as $cart)
                            <li class="item">
                                <div class="ui-box">
                                    <div class="imgProduct"><a href="/product/show?id={{$cart->product['id']}}"><img
                                                    src="{{$cart->product['thumb']}}"></a></div>
                                    <div class="info ui-box-flex">
                                        <div class="name">
                                            <span>{{$cart->product['name']}}</span>
                                        </div>
                                        <div class="price">
                                            <p>
                                                <span>售价：</span><span>{{doubleval($cart->product['price'])}}元</span>
                                                <span>合计：</span><span class="hj">{{doubleval($cart->product['price'] * $cart->num)}}
                                                    元</span>
                                            </p>
                                            <div class="tip">
                                                <span style="display: none;">请于{{$cart->creeated_at}}前下单，逾期将失效。</span>
                                            </div>
                                        </div>
                                        <div class="num" data-id="{{$cart->id}}"
                                             data-price="{{$cart->product['price']}}">
                                            <div class="xm-input-number">
                                                <div class="input-sub "></div>
                                                <div class="input-num"><span>{{$cart->num}}</span></div>
                                                <div class="input-add active "></div>
                                            </div>
                                            <div class="delete">
                                                <a href="javascript:;">
                                                    <span class="icon-iconfontshanchu"></span>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="append"></div>
                            </li>
                        @endforeach

                    </ul>
                </div>
                <div class="pointBox">
                    <div class="point" style="display: none;"><span class="act act_special">包邮</span><span></span></div>
                    <div class="point">
                        <p>温馨提示：产品是否购买成功，以最终下单为准，请尽快结算</p>
                    </div>
                </div>

                <!-- Navbar -->
                <div class="bottom-submit ui-box">
                    <div class="price"><span id="num">共{{$count['num']}}件 金额：</span><br><strong
                                id="total_price">{{$count['total_price']}}</strong><span>元</span>
                    </div>
                    <div class="btn"><a class="ui-button ui-button-disable"
                                        href="/product/category"><span>继续购物</span></a></div>
                    <div class="btn"><a class="ui-button"
                                        href="/order/check_out"><span>去结算</span></a></div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script src="/wechat/assets/js/jquery.min.js"></script>
    <script src="/wechat/assets/js/common.js"></script>

    <script>
        $(function () {
            //增加
            $('.input-add').click(function () {
                var $num = $(this).siblings('.input-num').children('span');
                var $sub = $(this).siblings('.input-sub');
                var price = $(this).parents('.num').data('price');


                var num = $num.text();
                num = parseInt(num) + 1;
                $num.text(num);

                var hj = num * price;
                $(this).parents('.info').find('.hj').text(hj + '元');
                if (!$sub.hasClass('active')) {
                    $sub.addClass('active');
                }
                $.ajax({
                    type: 'PATCH',
                    url: '/cart/add',
                    data: {id: $(this).parents('.num').data('id')},
                    success: function (data) {
                        $('#num').text('共' + data.num + '件 金额:');
                        $('#total_price').text(data.total_price);
                    }
                });

            })
            //减少
            $('.input-sub').click(function () {
                var $num = $(this).siblings('.input-num').children('span');
                var num = $num.text();
                var price = $(this).parents('.num').data('price');
                if (num == 1) {
                    return false;
                }
                num = parseInt(num) - 1;
                if (num == 1) {
                    $(this).removeClass('active');
                }
                $num.text(num);
                var hj = num * price;
                $(this).parents('.item').find('.hj').text(hj);
                $.ajax({
                    type: 'PATCH',
                    url: '/cart/sub',
                    data: {id: $(this).parents('.num').data('id')},
                    success: function (data) {
                        $('#num').text('共' + data.num + '件 金额:');
                        $('#total_price').text(data.total_price);
                    }
                });
            })
            //删除商品
            $('.icon-iconfontshanchu').click(function () {
                var id = $(this).parents('.num').data('id');
                var _this = $(this);
                $.ajax({
                    type: 'DELETE',
                    url: '/cart/delete_cart',
                    data: {id: id},
                    success: function (data) {
                        var length = $('.item').length;
                        if (length == 1) {
                            $('#more').show();
                            $('#carts').hide();
                            return false;
                        }
                        $("#num").text("共" + data.num + "件 金额:");
                        $("#total_price").text(data.total_price);
                        _this.parents('.item').remove();
                    }
                });
            })

        })


    </script>
@endsection


